﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Common.Utilities;
using Common.Utilities.Dispatcher;

namespace Workflows.Components.TextMining.Clustering
{
    /// <summary>
    /// 
    /// </summary>
    public partial class DocumentTermListEditor : Form,IComponentEditor
    {
        #region fields

        private object _ActivityNode;
        private DocumentTermListComponent _DocTermListComponent;
        #endregion

        #region init
        /// <summary>
        /// 
        /// </summary>
        public DocumentTermListEditor()
        {
            InitializeComponent();
        }

        private void DocumentTermListEditor_Load(object sender, EventArgs e)
        {

        }
        #endregion

        #region IComponentEditor Members

        public event UpdateComponentSettings UpdateComponentSettingEvent;

        public object OwnerActivityNode
        {
            get { return _ActivityNode; }
            set { _ActivityNode=value; }
        }

        public Type WorkflowComponentType
        {
            get { return typeof (DocumentTermListComponent); }
        }

        public void LoadSettings(GlobalVariable[] globalVars, WorkflowComponentBase[] fromComponents,
                                 object activityNode, WorkflowComponentBase wfComponent)
        {
            this._ActivityNode = activityNode;
            this._DocTermListComponent = (DocumentTermListComponent) wfComponent;

            this.txt_ConnStr.Text = this._DocTermListComponent.ConnectionString;
            this.rt_Sql.Text = this._DocTermListComponent.SelectSqlStatement;

            this.cbo_DocIDField.Items.Clear();
            this.cbo_TermIDField.Items.Clear();
            this.cbo_TermField.Items.Clear();
            this.cbo_TermWeight.Items.Clear();
            this.cbo_DocLabelField.Items.Clear();
            this.cbo_DocContentField.Items.Clear();

            if(!string.IsNullOrEmpty(this._DocTermListComponent.ConnectionString) && 
                !string.IsNullOrEmpty(this._DocTermListComponent.SelectSqlStatement))
            {
                string sql = this._DocTermListComponent.SelectSqlStatement;
                if (sql.IndexOf("{0}") > 0 && (!string.IsNullOrEmpty(this._DocTermListComponent.ApplListFilePath)))
                {
                    List<int> applIDs = DocumentTermListWorker.GetApplIDsFromFile(this._DocTermListComponent.ApplListFilePath);
                    if (applIDs.Count > 0)
                    {
                        int firstApplID = applIDs[0];
                        sql = sql.Replace("{0}", firstApplID.ToString());
                    }
                }

                TableColumn[] cols = DbUtil.GetTableColumns(DataProviderType.MSSQL,
                                                            this._DocTermListComponent.ConnectionString,
                                                            sql);
                if (cols != null && cols.Length > 0)
                {
                    foreach (TableColumn col in cols)
                    {
                        this.cbo_DocIDField.Items.Add(col.ColumnName);
                        this.cbo_TermIDField.Items.Add(col.ColumnName);
                        this.cbo_TermField.Items.Add(col.ColumnName);
                        this.cbo_TermWeight.Items.Add(col.ColumnName);
                        this.cbo_DocLabelField.Items.Add(col.ColumnName);
                        this.cbo_DocContentField.Items.Add(col.ColumnName);
                    }
                }
            }
            if(this.cbo_DocIDField.Items.Count>0 && 
                !string.IsNullOrEmpty(this._DocTermListComponent.DocIDField))
            {
                for(int i=0;i<this.cbo_DocIDField.Items.Count;i++)
                {
                    if(this.cbo_DocIDField.Items[i].ToString()==this._DocTermListComponent.DocIDField)
                    {
                        this.cbo_DocIDField.SelectedIndex = i;
                        break;
                    }
                }
            }
            if (this.cbo_DocIDField.Items.Count > 0 &&
                !string.IsNullOrEmpty(this._DocTermListComponent.DocIDField))
            {
                for (int i = 0; i < this.cbo_DocIDField.Items.Count; i++)
                {
                    if (this.cbo_DocIDField.Items[i].ToString() == this._DocTermListComponent.DocIDField)
                    {
                        this.cbo_DocIDField.SelectedIndex = i;
                        break;
                    }
                }
            }

            if (this.cbo_TermIDField.Items.Count > 0 &&
                !string.IsNullOrEmpty(this._DocTermListComponent.TermIDField))
            {
                for (int i = 0; i < this.cbo_TermIDField.Items.Count; i++)
                {
                    if (this.cbo_TermIDField.Items[i].ToString() == this._DocTermListComponent.TermIDField)
                    {
                        this.cbo_TermIDField.SelectedIndex = i;
                        break;
                    }
                }
            }

            if (this.cbo_TermField.Items.Count > 0 &&
                !string.IsNullOrEmpty(this._DocTermListComponent.TermField))
            {
                for (int i = 0; i < this.cbo_TermField.Items.Count; i++)
                {
                    if (this.cbo_TermField.Items[i].ToString() == this._DocTermListComponent.TermField)
                    {
                        this.cbo_TermField.SelectedIndex = i;
                        break;
                    }
                }
            }

            if (this.cbo_TermWeight.Items.Count > 0 &&
                   !string.IsNullOrEmpty(this._DocTermListComponent.TermWeightField))
            {
                for (int i = 0; i < this.cbo_TermWeight.Items.Count; i++)
                {
                    if (this.cbo_TermWeight.Items[i].ToString() == this._DocTermListComponent.TermWeightField)
                    {
                        this.cbo_TermWeight.SelectedIndex = i;
                        break;
                    }
                }
            }

            if (this.cbo_DocLabelField.Items.Count > 0 &&
                   !string.IsNullOrEmpty(this._DocTermListComponent.DocLabelField))
            {
                for (int i = 0; i < this.cbo_DocLabelField.Items.Count; i++)
                {
                    if (this.cbo_DocLabelField.Items[i].ToString() == this._DocTermListComponent.DocLabelField)
                    {
                        this.cbo_DocLabelField.SelectedIndex = i;
                        break;
                    }
                }
            }

            if (this.cbo_DocContentField.Items.Count > 0 &&
                   !string.IsNullOrEmpty(this._DocTermListComponent.DocContentField))
            {
                for (int i = 0; i < this.cbo_DocContentField.Items.Count; i++)
                {
                    if (this.cbo_DocContentField.Items[i].ToString() == this._DocTermListComponent.DocContentField)
                    {
                        this.cbo_DocContentField.SelectedIndex = i;
                        break;
                    }
                }
            }

            this.txt_OutputFilePath.Text = this._DocTermListComponent.DocTermListResultFilePath;

            if(this._DocTermListComponent.TermWeightHasExclusion)
            {
                this.ck_TermWeightNotEqual.Checked = true;
                this.num_ExcludeTermWeight.Value = (decimal)this._DocTermListComponent.ExcludedTermWeight;
            }
            else
            {
                this.ck_TermWeightNotEqual.Checked = false;
            }
            if(this._DocTermListComponent.TermWeightHasFloor)
            {
                this.ck_TermWeightGreaterThan.Checked = true;
                this.num_MinTermWeight.Value = (decimal)this._DocTermListComponent.MinTermWeight;
            }
            else
            {
                this.ck_TermWeightGreaterThan.Checked = false;
            }
            if(this._DocTermListComponent.TermWeightHasCeiling)
            {
                this.ck_TermWeightLessThan.Checked = true;
                this.num_MaxTermWeight.Value = (decimal) this._DocTermListComponent.MaxTermWeight;
            }
            else
            {
                this.ck_TermWeightLessThan.Checked = false;
            }

            this.txt_ApplListFilePath.Text = this._DocTermListComponent.ApplListFilePath;
            if (this._DocTermListComponent.DbProvider == DataProviderType.MSSQL)
                this.cbo_DbProviders.SelectedIndex = 0;
            else
                this.cbo_DbProviders.SelectedIndex = 1;
        }

        public void SaveSettings()
        {
            this._DocTermListComponent.ConnectionString = this.txt_ConnStr.Text;
            this._DocTermListComponent.SelectSqlStatement = this.rt_Sql.Text;
            this._DocTermListComponent.DocIDField = this.cbo_DocIDField.Text;
            this._DocTermListComponent.TermIDField = this.cbo_TermIDField.Text;
            this._DocTermListComponent.TermField = this.cbo_TermField.Text;
            this._DocTermListComponent.TermWeightField = this.cbo_TermWeight.Text;
            this._DocTermListComponent.DocLabelField = this.cbo_DocLabelField.Text;
            this._DocTermListComponent.DocContentField = this.cbo_DocContentField.Text;
            this._DocTermListComponent.DocTermListResultFilePath = this.txt_OutputFilePath.Text;

            if (this.ck_TermWeightNotEqual.Checked)
            {
                this._DocTermListComponent.TermWeightHasExclusion = true;
                this._DocTermListComponent.ExcludedTermWeight = (double) this.num_ExcludeTermWeight.Value;
            }
            else
            {
                this._DocTermListComponent.TermWeightHasExclusion = false;
            }
            if (this.ck_TermWeightLessThan.Checked)
            {
                this._DocTermListComponent.TermWeightHasCeiling = true;
                this._DocTermListComponent.MaxTermWeight = (double) this.num_MaxTermWeight.Value;
            }
            else
            {
                this._DocTermListComponent.TermWeightHasCeiling = false;
            }
            if (this.ck_TermWeightGreaterThan.Checked)
            {
                this._DocTermListComponent.TermWeightHasFloor = true;
                this._DocTermListComponent.MinTermWeight = (double) this.num_MinTermWeight.Value;
            }
            else
            {
                this._DocTermListComponent.TermWeightHasFloor = false;
            }

            this._DocTermListComponent.ApplListFilePath = this.txt_ApplListFilePath.Text;
            this._DocTermListComponent.DbProvider = DataProviderType.MSSQL;
            if (this.cbo_DbProviders.SelectedIndex == 1)
                this._DocTermListComponent.DbProvider = DataProviderType.Oracle;
        }

        public TestResult Test()
        {
            throw new NotImplementedException();
        }

        #endregion

        private void btn_Refresh_Click(object sender, EventArgs e)
        {
            if(!string.IsNullOrEmpty(this.txt_ConnStr.Text) && 
                !string.IsNullOrEmpty(this.rt_Sql.Text))
            {
                this.cbo_DocIDField.Items.Clear();
                this.cbo_TermIDField.Items.Clear();
                this.cbo_TermField.Items.Clear();
                this.cbo_TermWeight.Items.Clear();
                this.cbo_DocLabelField.Items.Clear();
                this.cbo_DocContentField.Items.Clear();

                string sql = this.rt_Sql.Text;
                if(sql.IndexOf("{0}")>0 && (!string.IsNullOrEmpty(this.txt_ApplListFilePath.Text)))
                {
                    List<int> applIDs = DocumentTermListWorker.GetApplIDsFromFile(this.txt_ApplListFilePath.Text);
                    if(applIDs.Count>0)
                    {
                        int firstApplID = applIDs[0];
                        sql = sql.Replace("{0}", firstApplID.ToString());
                    }
                }

                TableColumn[] cols =
                    DbUtil.GetTableColumns(DataProviderType.MSSQL,
                                           this.txt_ConnStr.Text,
                                           sql);
                if (cols != null && cols.Length > 0)
                {
                    foreach (TableColumn col in cols)
                    {
                        this.cbo_DocIDField.Items.Add(col.ColumnName);
                        this.cbo_TermIDField.Items.Add(col.ColumnName);
                        this.cbo_TermField.Items.Add(col.ColumnName);
                        this.cbo_TermWeight.Items.Add(col.ColumnName);
                        this.cbo_DocLabelField.Items.Add(col.ColumnName);
                        this.cbo_DocContentField.Items.Add(col.ColumnName);
                    }
                }

                if (this.cbo_DocIDField.Items.Count > 0 &&
                !string.IsNullOrEmpty(this._DocTermListComponent.DocIDField))
                {
                    for (int i = 0; i < this.cbo_DocIDField.Items.Count; i++)
                    {
                        if (this.cbo_DocIDField.Items[i].ToString() == this._DocTermListComponent.DocIDField)
                        {
                            this.cbo_DocIDField.SelectedIndex = i;
                            break;
                        }
                    }
                }

                if (this.cbo_TermIDField.Items.Count > 0 &&
                    !string.IsNullOrEmpty(this._DocTermListComponent.TermIDField))
                {
                    for (int i = 0; i < this.cbo_TermIDField.Items.Count; i++)
                    {
                        if (this.cbo_TermIDField.Items[i].ToString() == this._DocTermListComponent.TermIDField)
                        {
                            this.cbo_TermIDField.SelectedIndex = i;
                            break;
                        }
                    }
                }

                if (this.cbo_TermField.Items.Count > 0 &&
                    !string.IsNullOrEmpty(this._DocTermListComponent.TermField))
                {
                    for (int i = 0; i < this.cbo_TermField.Items.Count; i++)
                    {
                        if (this.cbo_TermField.Items[i].ToString() == this._DocTermListComponent.TermField)
                        {
                            this.cbo_TermField.SelectedIndex = i;
                            break;
                        }
                    }
                }

                if (this.cbo_TermWeight.Items.Count > 0 &&
                   !string.IsNullOrEmpty(this._DocTermListComponent.TermWeightField))
                {
                    for (int i = 0; i < this.cbo_TermWeight.Items.Count; i++)
                    {
                        if (this.cbo_TermWeight.Items[i].ToString() == this._DocTermListComponent.TermWeightField)
                        {
                            this.cbo_TermWeight.SelectedIndex = i;
                            break;
                        }
                    }
                }

                if (this.cbo_DocLabelField.Items.Count > 0 &&
                   !string.IsNullOrEmpty(this._DocTermListComponent.DocLabelField))
                {
                    for (int i = 0; i < this.cbo_DocLabelField.Items.Count; i++)
                    {
                        if (this.cbo_DocLabelField.Items[i].ToString() == this._DocTermListComponent.DocLabelField)
                        {
                            this.cbo_DocLabelField.SelectedIndex = i;
                            break;
                        }
                    }
                }

                if (this.cbo_DocContentField.Items.Count > 0 &&
                   !string.IsNullOrEmpty(this._DocTermListComponent.DocContentField))
                {
                    for (int i = 0; i < this.cbo_DocContentField.Items.Count; i++)
                    {
                        if (this.cbo_DocContentField.Items[i].ToString() == this._DocTermListComponent.DocContentField)
                        {
                            this.cbo_DocContentField.SelectedIndex = i;
                            break;
                        }
                    }
                }
            }
        }

        private void btn_OK_Click(object sender, EventArgs e)
        {
            this.SaveSettings();
            if(this.UpdateComponentSettingEvent !=null)
            {
                this.UpdateComponentSettingEvent(this._ActivityNode, this._DocTermListComponent);
            }

            this.Close();
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btn_Browse_Click(object sender, EventArgs e)
        {
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Filter = "Xml Files(*.xml)|*.xml|All files(*.*)|*.*";
            if(dlg.ShowDialog()==System.Windows.Forms.DialogResult.OK)
            {
                this.txt_OutputFilePath.Text = dlg.FileName;
            }
        }

        private void btn_EditExcludedTerms_Click(object sender, EventArgs e)
        {
            ExcludedTermsEditor exTermEditor = new ExcludedTermsEditor();
            exTermEditor.ExcludedTerms = this._DocTermListComponent.ExcludedTerms;
            exTermEditor.OnExcludedTermsUpdated += new ExcludedTermsUpdated(exTermEditor_OnExcludedTermsUpdated);
            exTermEditor.Show(this);
        }

        void exTermEditor_OnExcludedTermsUpdated(List<string> excludedTerms)
        {
            this._DocTermListComponent.ExcludedTerms = excludedTerms;
        }

        private void ck_TermWeightNotEqual_CheckedChanged(object sender, EventArgs e)
        {
            if(this.ck_TermWeightNotEqual.Checked)
                this.num_ExcludeTermWeight.Enabled = true;
            else
                this.num_ExcludeTermWeight.Enabled = false;
        }

        private void ck_TermWeightGreaterThan_CheckedChanged(object sender, EventArgs e)
        {
            if(this.ck_TermWeightGreaterThan.Checked)
                this.num_MinTermWeight.Enabled = true;
            else
                this.num_MinTermWeight.Enabled = false;
        }

        private void ck_TermWeightLessThan_CheckedChanged(object sender, EventArgs e)
        {
            if(this.ck_TermWeightLessThan.Checked)
                this.num_MaxTermWeight.Enabled = true;
            else
                this.num_MaxTermWeight.Enabled = false;
        }

        private void btn_BrowseApplListFile_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg=new OpenFileDialog();
            dlg.Filter = "Text files(*.txt)|*.txt|All files(*.*)|*.*";
            if(dlg.ShowDialog()==System.Windows.Forms.DialogResult.OK)
                this.txt_ApplListFilePath.Text = dlg.FileName;
        }

        private void btn_BrowseExcelFile_Click(object sender, EventArgs e)
        {
            DocumentTermProvider termProvider=new DocumentTermProvider();
            termProvider.Show();
        }

    }
}
